#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int eraseOverlapIntervals(vector<vector<int>> &intervals)
    {
        if (intervals.size() == 1)
            return 0;
        sort(intervals.begin(), intervals.end());
        int left = intervals[0][0];
        int right = intervals[0][1], ret = 0;
        for (int i = 1; i < intervals.size(); i++)
        {
            int curl = intervals[i][0];
            int curr = intervals[i][1];
            if (curl < right)
            {
                // 此时有重叠区间一定要进行删除
                ret++;
                // 删除考虑删除右边端点大的
                if (curr >= right)
                {
                    if (curr == right)
                    {
                        left = curl;
                        right = curr;
                    }
                }
                else
                {
                    left = curl;
                    right = curr;
                }
            }
            else
            {
                left = curl;
                right = curr;
            }
        }
        return ret;
    }
};